package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.ZiXun_Skill_Adapter;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.Collection;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main_my_zixun_SkillActivity extends Activity implements BGARefreshLayoutDelegate {
    /**
     * 我咨询的技术页面
     */
    private RelativeLayout relative_my_zixun_skill_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private ArrayList<Collection> list = new ArrayList<Collection>();
    private BGARefreshLayout my_skill_zixun_BGARefreshLayout;
    private ListView my_skill_zixun_listview;
    private int page = 1;
    private ZiXun_Skill_Adapter mZiXun_Skill_Adapter;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                my_skill_zixun_listview.setAdapter(mZiXun_Skill_Adapter);
                my_skill_zixun_BGARefreshLayout.endRefreshing();
            }
            if (msg.what == 2) {
                mZiXun_Skill_Adapter.notifyDataSetChanged();
                my_skill_zixun_BGARefreshLayout.endLoadingMore();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main_my_zixun__skill);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();

        relative_my_zixun_skill_back = (RelativeLayout) findViewById(R.id.relative_my_zixun_skill_back);
        my_skill_zixun_BGARefreshLayout = (BGARefreshLayout) findViewById(R.id.my_skill_zixun_BGARefreshLayout);
        my_skill_zixun_listview = (ListView) findViewById(R.id.my_skill_zixun_listview);
        my_skill_zixun_BGARefreshLayout.setDelegate(this);

        mZiXun_Skill_Adapter = new ZiXun_Skill_Adapter(this, list, Contant.come_from_my_skill_zixun);
        my_skill_zixun_BGARefreshLayout.setRefreshViewHolder(new com.example.xianji.BGARefresh.BGANormalRefreshViewHolder(this, true));

        relative_my_zixun_skill_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_my_zixun_SkillActivity.this.finish();
            }
        });

        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_collection_data();
            }
        });

    }

    public void get_collection_data() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("page", 1);
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.my_zixun_skill,
                params, new Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        list.clear();
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() == 0) {
                            Toast.makeText(Main_my_zixun_SkillActivity.this, response.getString("msg"), Toast.LENGTH_SHORT).show();
                        } else {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject obj = array.getJSONObject(i);
                                Collection mCollection = new Collection();
                                mCollection.setTitle(obj.getJSONObject("achieveuser").getString("title"));
                                mCollection.setCreated_at(obj.getString("created_at"));
                                mCollection.setDetail(obj.getString("detail"));
                                mCollection.setId(obj.getInt("achieve_id"));
                                list.add(mCollection);
                            }
                        }
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    public void get_collection_data_more() {
        page++;
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("page", page);
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.my_zixun_skill,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() == 0) {
                            Toast.makeText(Main_my_zixun_SkillActivity.this, "没有更多数据哦", Toast.LENGTH_SHORT).show();
                            my_skill_zixun_BGARefreshLayout.endLoadingMore();
                        } else {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject obj = array.getJSONObject(i);
                                Collection mCollection = new Collection();
                                mCollection.setTitle(obj.getJSONObject("achieveuser").getString("title"));
                                mCollection.setCreated_at(obj.getString("created_at"));
                                mCollection.setDetail(obj.getString("detail"));
                                mCollection.setId(obj.getInt("achieve_id"));
                                list.add(mCollection);
                            }
                        }
                        handler.sendEmptyMessage(2);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        get_collection_data();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        get_collection_data_more();
        return true;
    }
}
